package com.sinosoft.perfect.system._authorization.dao;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseSqlSessionDaoSupport;
import com.sinosoft.perfect.system._authorization.MapperSystemAuthorization;
import com.sinosoft.perfect.system._authorization.vo.RoleResourceRelationAuthorize;

public class RoleResourceAuthorizeDaoImpl extends BaseSqlSessionDaoSupport implements IRoleResourceAuthorizeDao {

	private MapperSystemAuthorization getMapperSystemAuthorization(){
		return getSqlSession().getMapper(MapperSystemAuthorization.class);
	}
	
	@Override
	public void insertByRoleResourceRelationAuthorize(
			RoleResourceRelationAuthorize roleResourceRelationAuthorize)
			throws SQLException {
		getMapperSystemAuthorization().insertByRoleResourceRelationAuthorize(roleResourceRelationAuthorize);
	}

	@Override
	public RoleResourceRelationAuthorize queryTRoleResourceRelationByResourceAndRole(
			RoleResourceRelationAuthorize roleResourceRelationAuthorizeParam)
			throws SQLException {
		List<RoleResourceRelationAuthorize> roleResourceRelationAuthorizes = getMapperSystemAuthorization().
			queryTRoleResourceRelationByResourceAndRole(roleResourceRelationAuthorizeParam);
		if (roleResourceRelationAuthorizes != null && roleResourceRelationAuthorizes.size() > 0) {
			return (getMapperSystemAuthorization().queryTRoleResourceRelationByResourceAndRole(roleResourceRelationAuthorizeParam).get(0));
		}else {
			return null;
		}
	}

	@Override
	public int updatePowerOfRoleResource(RoleResourceRelationAuthorize roleResourceRelationAuthorize)
			throws SQLException {
		return getMapperSystemAuthorization().updatePowerOfRoleResource(roleResourceRelationAuthorize);
	}

	@Override
	public int deletePowerOfRoleResource(RoleResourceRelationAuthorize roleResourceRelationAuthorize)
			throws SQLException {
		return getMapperSystemAuthorization().deletePowerOfRoleResource(roleResourceRelationAuthorize);
	}

	
	@Override
	public List<String> queryResourceIdsOfRole(String roleSystemId)
			throws SQLException {
		return getMapperSystemAuthorization().queryResourceIdsOfRole(roleSystemId);
	}

	@Override
	public void operateResourceRoleAuthorize(List<RoleResourceRelationAuthorize> deleteList, List<RoleResourceRelationAuthorize> insertList) 
			throws SQLException {
		if(deleteList != null && deleteList.size()>0){
			for(int i=0;i<deleteList.size();i++){
				getMapperSystemAuthorization().deletePowerOfRoleResource(deleteList.get(i));
			}
		}
		if(insertList != null && insertList.size()>0){
			for(int i=0;i<insertList.size();i++){
				getMapperSystemAuthorization().insertByRoleResourceRelationAuthorize(insertList.get(i));
			}
		}
	}
	
	
}
